Welcome.
In this lecture, we're going to explore the difference between server scripts and local scripts.
As many beginners get lost when to use these two different scripts, what's important to understand
is that Roblox is an online game.
Meaning whenever you join an experience, you download a copy of that game onto your computer, meaning
you download all the audio, the parts, the scripts, and so on.
Your computer is rendering a copy of the game that is separate from the server.
Whenever the server makes changes to the game, the server tells you to do that as well on your copy,
the server or server scripts, which I'll create real quick here in the workspace.
These scripts make changes to the game that all players can see.
This can be things such as updating leaderboards, spawning in NPCs, changing maps, giving players
tools, and so on.
The purpose of server scripts is for you to code logic in your game that needs to affect all players.
There also used to verify information coming from clients through remote events, which we're going
to talk about more in a future course.
Most of the time your server scripts are going to be located in the server script service container,
and this is a special service used for organizing our server scripts.
No clients or players in our game have access to this service.
It's reserved only for the server.
Sometimes you may want to put server scripts inside of the workspace as well, which is fine, but most
of the time you'll want to put them inside of the server script service.
We also have the server storage service.
If we ever need to store any objects in our game that do not need to be replicated to all players.
The service is also private and can only be accessed by the server.
Now a local script, which I'll create real quick.
Here in the workspace, a local script is used for when we want to affect only our copy of the game.
Local scripts have access to client side features, such as listening to inputs from your mouse and
keyboard.
The server is unable to do this because the server is separate from your computer being hosted on a
computer or somewhere else in the world.
Therefore, server scripts are unable to listen to inputs from your computer.
If we wanted to script the functionality for our GUI on our screen, we would have to use a local script
if we wanted to cause a change in our game's workspace that is only visible on our copy of the game.
Again, we would have to use a local script if we wanted to manipulate our camera.
We must use a local script.
Any changes made with a local script is only visible to our copy of the game, and it does not replicate
to the server or any other players.
For a local script to execute, we must place them in a valid container.
Services.
These services are either the starter pack, which we use for tools, starter GUI which is used for
screen GUI, and then inside of starter player.
We have starter player scripts, which we use for general client side functionality, such as listening
for when our player dies and displaying a death message in the screen and stuff like that.
And then we have a starter character scripts, which is used for affecting our player's character,
like health regeneration.
When our player's character dies, any character scripts get deleted and a new set of those scripts
get placed in the player's new character.
The last container we can place local scripts inside of is going to be replicated first.
Typically, you only use local scripts in here for loading screens when your player first joins the
game.
Otherwise, we don't use replicated first for storing local scripts.
So if a local script is not stored and these five containers, then they're not going to execute.
The only exception would be scripts that are stored in a player's character model, but otherwise we
cannot execute any local scripts that are in, for example, replicated storage.
We can't execute them if they're in the lighting service or if they're in the workspace.
It's only within these particular services.
So to recap, we use server scripts to cause changes that all players can see, and we use local scripts
to cause changes that only one player can see on their copy of the game.
So let's go ahead and take a look at the difference between local scripts and server scripts.
What I'm going to do is I'm going to take this local script that I've created in the workspace, and
I'm going to change the parent property to be inside of starter player scripts inside of my local script.
All I'm going to do is create a new variable.
I'm going to call it part, and I want to create a new part in my game.
And we can do so using the instance dot new function.
And then we pass the class name of the new object we would like to create.
In this case we would like to create a new part.
From this point we can refer to this part.
And I want to change the parent of this part equal to the workspace.
That way it renders on our screen inside of the game.
And then I also want to make sure that it is anchored.
So physics are not being applied.
So we'll set anchor to true.
And then we're going to change the position of our part.
Equal to.
And we're going to use something called a vector three dot new.
We're going to learn more about vector three later.
But just for now we've got to pass an x, y and z position so we could set it at like ten, ten and
ten.
So now this local script, whenever a player joins our game, we're going to get a copy of this local
script on our computer.
And we're going to be executing all of the code inside of this local script.
And this is going to happen on our copy of the game.
So we're going to create a new part.
We're setting the parent of that part equal to the workspace.
And we're setting it at the position of ten, ten, ten inside of the workspace.
So if we go ahead and play test our game by hitting play.
And we try to look for our part.
There it is.
We have a new part inside of the workspace, and it has spawned at the position of ten, ten, ten.
However, if we go ahead and change to the server when we look on the server here, as you can see,
we cannot see our part anywhere.
And that's because again, that local script is only affecting our copy of the game.
The server can't see it, it's only visible on our game right here.
And that means we can also interact with this part, such as jumping on it.
So here we are.
I'm standing on my part, but if I go and look on the server, as you can see, the server can't see
the part.
There's nothing here.
And it just looks like our player's character is floating.
Now.
Instead what I'm going to do as I'm going to copy this code.
And I'm going to delete it out of here and then place it inside of my server script.
And this is going to do the exact same thing.
But this time since the server is creating it, it should be visible for my copy of the game and it
should also be visible on the server as well.
So if we go ahead and hit play.
And we go and look around.
There we are.
There is our part.
And if we switch to the view on the server, as you can see, the server can see it as well.
Now, let's say I had a local script here on the client that went and deleted this part.
So I'm just going to select it here and I'm just going to delete the part.
So I'm going to hit delete on my keyboard.
Now it is gone out of our copy of the game.
And then if I go back on the server as you can see it's still here.
Right?
But on our local copy of the game, we don't see the part anymore because we deleted it.
And again, this applies for all properties and objects in our game.
Whenever a server script makes a change to an object or a property, all players in the game will observe
that change.
But if a player on their local copy of the game makes a change to an object or a property through a
local script, it's only going to be visible on that player's copy of the game.
No other player is going to be able to see it, and the server is not going to be able to see it either.
And this works, as I said, for all properties.
So for example, if I were to refer to the workspace and get the spawn location and let's say I wanted
to change the color to something new, and we do so using a color three dot new.
Again, we're going to be learning about these data types later.
But for now I'm just going to change it to let's say we want to change it to a blue color.
So then we go and playtest our game again.
As you can see, our spawn location is now blue, but if we go look at the server, it's also blue,
and then we go back to the client.
If the client through a script decides to change the color.
Let's say we change it to green.
We can see that it's green on our copy of the game, but if we go and look on the server, it is still
blue.
All righty.
I hope that cleared up the differences between local scripts and server scripts.
That's all for me for this lecture, and I will see you in the next one.